<?php

/* ========================================================================= */
/* This file is part of the HypnoPHP Framework Library                       */
/* Copyright (c) 2009-2010, Radu Buzea                                       */
/* ========================================================================= */

/**
* @author Radu Buzea
* @copyright Copyright (c) 2009-2010, Radu Buzea (www.hypnosis.ro)
* @license GNU Lesser General Public License (www.gnu.org/copyleft/lesser.html)
* @package HypnoPHP
* @category Core
* @version 1.0.3
*/

// ============================================================================
// Interfaces: IBaseDbResult
// ============================================================================

interface IBaseDbResult
{
	public function isReady();
	public function assign($db_result = null);
	public function columns();
	public function columnExists($column_name = '');
	public function columnNames($include_table_name = false);
	public function columnsRows($assoc = false);
	public function countColumns();
	public function countRows();
	public function row($assoc = false);
	public function rows($assoc = false);
}

// ============================================================================
// Abstract class: HBaseDbResult
// ============================================================================

abstract class HBaseDbResult extends HbaseObject implements IBaseDbResult
{
	protected $__ready_state = 0;
	protected $__database_object = null;
	protected $__database_result = null;
	protected $__rows = array();
	protected $__cols = array();

	# ###########################################################################

	/**
	* Initialize object properties
	*
	* @return void
	*/
	public function __initialize()
	{
		$this->__ready_state = 0;
		$this->__database_object = null;
		$this->__database_result = null;
		$this->__rows = array();
		$this->__cols = array();

		$args_num = func_num_args();
		if ($args_num == 0) { return; }
		$func_args = func_get_arg(0);
		if (is_array($func_args) && count($func_args) > 0) {
			foreach ($func_args as $argument) {
				if (is_object($argument)) {
					$this->database_object = $argument;
				} else if (is_resource($argument)) {
					$this->__database_result = $argument;
				}
			}
		}
	}

	# ###########################################################################

	/**
	* Database object property - getter method
	*
	* @return HBaseDb
	*/
	protected function __get_database_object()
	{
		return $this->__database_object;
	}

	# ###########################################################################

	/**
	* Database object property - setter method
	*
	* @param HBaseDb $param
	* @return void
	*/
	protected function __set_database_object($param = null)
	{
		if (is_null($param)) {
			$this->__ready_state = 0;
		} else if (is_object($param) && is_subclass_of($param, 'HBaseDb')) {
			$this->__database_object = $param;
			$this->__ready_state = 0;
		}
	}

	# ###########################################################################

	/**
	* Check if the object is ready to operate
	*
	* @return bool
	*/
	public function isReady()
	{
		if ($this->__ready_state > 0) { return ($this->__ready_state == 1); }
		$result = true;
		if (is_null($this->__database_result) || is_null($this->__database_object) || !is_resource($this->__database_result) ||
			!is_object($this->__database_object) || !is_subclass_of($this->__database_object, 'HBaseDb')) { $result = false; }
		$this->__ready_state = $result ? 1 : 2;
		return $result;
	}

	# ###########################################################################

	/**
	* Assign a database result resource
	*
	* @param resource $db_result
	* @return void
	*/
	public function assign($db_result = null)
	{
		if (is_null($db_result)) {
			$this->__database_result = null;
			$this->__ready_state = 0;
		} else if (is_resource($db_result)) {
			$this->__ready_state = 0;
			$this->__database_result = $db_result;
		}
	}

	# ###########################################################################

	/**
	* Get result columns and rows
	*
	* @param bool $assoc Get associated keys in the rows array
	* @return array
	*/
	public function columnsRows($assoc = false)
	{
		$columns = $this->columns();
		$rows = $this->rows($assoc);
		return array('columns' => $cols, 'rows' => $rows);
	}

	# ###########################################################################

}